/**
 *
 */
define(["dojo/_base/declare", "dojo/dom-construct", "dojo/_base/lang", "dojo/_base/array", "dijit/layout/ContentPane",
		"esri/map", "esri/SpatialReference", "esri/layers/ArcGISTiledMapServiceLayer", "esri/InfoTemplate",
		"widget/MapManager", "dojo/topic", "dojo/ready"
	],
	function(declare, domConstruct, lang, array, ContentPane, Map, SpatialReference, ArcGISTiledMapServiceLayer,InfoTemplate,
		MapManager, topic, ready) {
		return declare(ContentPane, {
			baseClass: "mapPane",
			/**
			 * 地图对象。
			 */
			map: null,
			basemap: [],
			/**
			 * 地图初始化参数。
			 */
			options: {
				logo: false,
				/*minZoom: 0,
				maxZoom: 7,
				zoom: 0,
				extent:MapManager.getExtent(),*/
				/* spatialReference: new SpatialReference({
					wkid: 4490
				}) */
			},
			constructor: function(params) {
				if(params && params.options) {
					params.options = lang.mixin(this.options, params.options);
				}
			},
			startup: function() {
				var self = this,id=self.options.id;
				var mapNode = domConstruct.create("div", {"style":"height:100%;width:100%;float:left;",id:id});
				this.containerNode.appendChild(mapNode);
				ready(function() {
					self.map = new Map(id, self.options);
					MapManager.addMap(self.map);
					self.addBaseLayer(self.basemap);
					array.forEach(self.layers, function(layer) {
						self.addLayer(layer);
					});
					self.map.on("load", function() {
						topic.publish("afterMapLoaded", {
							map: self.map
						});
					})
				});
				this.inherited(arguments);
			},
			addLayer: function(layer) {
		       var self = this;
               if(layer){
            	   layer = new ArcGISTiledMapServiceLayer(layer.url,layer);
    			   self.map.addLayer(layer);
            	   /*
            	   if(typeof layer.url === "string") {
            		   if(layer.infoTemplate && typeof layer.infoTemplate === "string"){
            			   layer.infoTemplate = require.toUrl("project/template/infoWindow/" + layer.infoTemplate+ "LayerContent.html");
            			   require(["dojo/text!" + layer.infoTemplate], function(content) {
            				 var infoTemplate = new InfoTemplate("详细信息", content);
            				   var infoTemplates = {
            						   0:{infoTemplate:infoTemplate}
            				       };
            				   layer = new ArcGISTiledMapServiceLayer(layer.url,layer);
            				   layer.setInfoTemplates(infoTemplates);
            				   self.map.addLayer(layer);
            			   });
            		   }else{
            			   layer = new ArcGISTiledMapServiceLayer(layer.url,layer);
            			   self.map.addLayer(layer);
            		   }
            	   }
               */}
			},
			setInfowindowEvt:function(){
				  $("#mainMap .esriPopup .titleButton.maximize").unbind('click').click(function() {
				  if ($(this).parents(".esriPopup").find(".contentPane table:first").next().is(":hidden")) {
		              $(this).parents(".esriPopup").find(".contentPane table:first").css("display","none");
						$(this).parents(".esriPopup").find(".contentPane table").next().css("display","block");
				  }else{
					  if ($(".sider")) {
		                  $(".sider").remove();
		              }
					  $(this).parents(".esriPopup").find(".contentPane table:first").css("display","block");
                    $(this).parents(".esriPopup").find(".contentPane table").next().css("display","none");
				  }

			  });
			},
			addBaseLayer: function(layers) {
				var self = this;
				array.forEach(layers, function(option) {
					var basemap = null,
						index = option.index;
					basemap = new ArcGISTiledMapServiceLayer(option.url, option);
					basemap.isBaseMap = true;
					if(index) {
						self.map.addLayer(basemap, index);
					} else {
						self.map.addLayer(basemap);
					}
				});
			},
			destroy: function() {
				this.inherited(arguments);
			}
		});
	});
